package cn.bigcontainer.alt.workflow.extension.impl;

import org.springframework.stereotype.Component;

import cn.bigcontainer.alt.meta.dto.MObjectDto;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import cn.bigcontainer.alt.meta.extension.MListDataFilter;
import cn.bigcontainer.alt.sys.dto.UserDto;
import cn.bigcontainer.alt.sys.util.SysUtil;

/**
 * 流程当前任务列表过滤器
 */
@Component
public class MyTaskListFilter implements MListDataFilter {

    @Override
    public String addWhere(MViewDto list, MViewDto search) {
        MObjectDto object = list.getApp().getObject();
        UserDto currentUser = SysUtil.getCurrentUserWithException(object.getRequest());
        String where = "%s.%s in (select process_instance_id from m_runtime_task task " +
                "left join m_runtime_task_candidate user on (user.runtime_task_id = task.id) " +
                "where user.candidate_id = '%d')";
        return String.format(where, object.getNo(), object.getKeyProperties().get(0).getName(), currentUser.getId());
    }

    @Override
    public boolean replace() {
        return false;
    }
}
